<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        const orders = [
            {
                id: 1,
                name: 'John Smith',
                address: '123 Main St',
                status: 'created',
                log: [
                    {
                        status: 'created',
                        time: '2023-02-16 12:30:00'
                    }
                ]
            },
            {
                id: 3,
                name: 'Jane Doe',
                address: '456 Second St',
                status: 'packed',
                log: [
                    {
                        status: 'created',
                        time: '2023-02-16 10:00:00'
                    },
                    {
                        status: 'packed',
                        time: '2023-02-16 12:30:00'
                    }
                ]
            },
            {
                id: 2,
                name: 'Bob Johnson',
                address: '789 Third St',
                status: 'shipped',
                log: [
                    {
                        status: 'created',
                        time: '2023-02-15 10:00:00'
                    },
                    {
                        status: 'packed',
                        time: '2023-02-15 12:00:00'
                    },
                    {
                        status: 'shipped',
                        time: '2023-02-16 9:00:00'
                    }
                ]
            }
        ];
        function getOrdersByStatus(order,created) {
           const arr=  order.filter(function (item) {
                for(let i=0;i< item.log.length;i++){
                    // console.log(item.log[i].status);
                    // console.log(item.status);
                    if( item.log[i]. status===created){
                        return true
                    }
                }
            })
            // 筛选的数组
            // return arr;
           const sortArr=  arr.sort(function(a,b){
                if(a.id<b.id){
                    return -1
                }
            })
            return sortArr
        }
        console.log(getOrdersByStatus(orders,'packed'));

      

        // 错误写法，forEach不能被打断
        // function getOrdersByStatus(status) {
        //  const arr=   status.filter(function (item) {
        //         item.log.forEach(function(item1){
        //             item.status=== item1.status
        //             console.log(item.status);
        //         })
        //         return
        //     })
        //     console.log(arr);
        // }
        // getOrdersByStatus(orders)
    </script>
</body>

</html>